package com.bana.sys.service;

import com.bana.sys.code.SystemLogType;
import com.bana.sys.code.ParameterKeys;
import com.bana.sys.dao.*;
import com.bana.sys.model.*;
import com.bana.sys.utils.Token;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 日志管理
 */
@Service
public class LogService {

    @Resource
    private SocketLogDAO socketLogDAO;
    @Resource
    private SystemLogDAO systemLogDAO;

    @Resource
    private ParameterService parameterService;

    /**
     * 添加系统日志
     * @param content 日志内容
     */
    public void insertSystemLog(String content)
    {
        try
        {
            SystemLog log=new SystemLog();
            log.setYhmc("system");
            log.setFunction(SystemLogType.Subscribe);
            log.setContent(content);
            log.setJoinId(null);
            systemLogDAO.insertLog(log);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    /**
     * 添加系统日志
     * @param token token
     * @param function 操作类型
     * @param content 日志内容
     * @param joinId 关联编号
     */
    public void insertSystemLog(String token,int function,String content,String joinId)
    {
        try
        {
            SystemLog log=new SystemLog();
            log.setYhmc(Token.getUser(token).getYhmc());
            log.setFunction(function);
            log.setContent(content);
            log.setJoinId(joinId);
            systemLogDAO.insertLog(log);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    /**
     * 添加发送日志
     * @param protocolId 协议编号
     * @param kzqbsm 控制器标识码
     * @param receive 接收内容
     */
    public void insertSocketLog(int protocolId, int kzqbsm,int dk, String receive) {
        try
        {
            SocketLog log=new SocketLog();
            log.setProtocolId(protocolId);
            log.setKzqbsm(kzqbsm);
            log.setReceive(dk+" "+receive);
            socketLogDAO.insertLog(log);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    /**
     * 添加发送日志
     * @param protocolId 协议编号
     * @param kzqbsm 控制器标识码
     * @param send 发送内容
     * @param result 发送结果
     * @param receive 接收内容
     * @param joinId 关联编号
     */
    public void insertSocketLog(int protocolId, int kzqbsm,int dk, String send, int result, String receive,String joinId) {
        try
        {
            SocketLog log=new SocketLog();
            log.setProtocolId(protocolId);
            log.setKzqbsm(kzqbsm);
            log.setSend(dk+" "+send);
            log.setResult(result);
            log.setReceive(receive);
            log.setJoinId(joinId);
            socketLogDAO.insertLog(log);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    /**
     * 删除系统日志
     */
    public void deleteSystemLogs()
    {
        int span=parameterService.getInteger(ParameterKeys.SystemLog);
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DATE,-span);
        calendar.set(Calendar.HOUR,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        systemLogDAO.deleteLog(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(calendar.getTime()));
        insertSystemLog(Token.SystemToken, SystemLogType.DeleteLog,String.format("delete system logs %d",span),null);
    }

    /**
     * 删除套接字日志
     */
    public void deleteSocketLog()
    {
        int span=parameterService.getInteger(ParameterKeys.SocketLog);
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DATE,-span);
        calendar.set(Calendar.HOUR,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        socketLogDAO.deleteLog(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(calendar.getTime()));
        insertSystemLog(Token.SystemToken, SystemLogType.DeleteLog,String.format("delete system logs %d",span),null);
    }

    public void insertExceptionLog(Exception e)
    {
        insertSystemLog(Token.SystemToken, SystemLogType.Exception,e.toString(),null);
    }

}
